package com.example.monitor.controller;

import com.example.monitor.model.DTO.UserAbnormalRecordsQueryDTO;
import com.example.monitor.model.DTO.UserAbnormalTypeQueryDTO;
import com.example.monitor.model.DTO.UserTrendQueryDTO;
import com.example.monitor.model.VO.*;
import com.example.monitor.service.UserAbnormalRecordsService;
import com.example.monitor.service.UserAbnormalTypeService;
import com.example.monitor.service.UserTrendService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Year;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final UserTrendService userTrendService;
    private final UserAbnormalTypeService userAbnormalTypeService;
    private final UserAbnormalRecordsService userAbnormalRecordsService;

    @GetMapping("/trend")
    public ResultVO<UserTrendVO> getUserTrend(
            @RequestParam String workerId,
            @RequestParam(required = false) Integer year) {
        UserTrendQueryDTO queryDTO = new UserTrendQueryDTO();
        queryDTO.setWorkerId(workerId);
        queryDTO.setYear(year != null ? year : Year.now().getValue());

        return userTrendService.getUserTrend(queryDTO);
    }

    @GetMapping("/abnormalType")
    public ResultVO<List<UserAbnormalTypeVO>> getUserAbnormalType(
            @RequestParam String workerId) {

        UserAbnormalTypeQueryDTO queryDTO = new UserAbnormalTypeQueryDTO();
        queryDTO.setWorkerId(workerId);

        return userAbnormalTypeService.getUserAbnormalTypeDistribution(queryDTO);
    }

    @GetMapping("/abnormalRecords")
    public ResultVO<PageResult<UserAbnormalRecordVO>> getUserAbnormalRecords(
            @RequestParam String workerId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        UserAbnormalRecordsQueryDTO queryDTO = new UserAbnormalRecordsQueryDTO();
        queryDTO.setWorkerId(workerId);
        queryDTO.setPage(page);
        queryDTO.setPageSize(pageSize);

        return userAbnormalRecordsService.getUserAbnormalRecords(queryDTO);
    }
}